  <?php
 class ForumsController extends ForumsAppController
 {
 	var $name = 'Forums';
 	var $uses = array("Forums.ForumCategory", "Forums.ForumThread", "Forum.ForumPost");

	/**
 	 * @var SessionComponent
 	 */
 	var $Session;
 	 /**
 	 * @var AclComponent
 	 */
 	var $Acl;
 	 /**
 	 * @var AuthComponent
 	 */
 	var $Auth;

 	var $paginate = array('ForumPost' =>
 							array(
 								'contain' => array('User')
 							));

	function index($slug = null)
	{
		$this->set('categories', $this->ForumCategory->fetchCategories($slug));
		if ($slug != null)
			$this->set('category', $this->ForumCategory->findBySlug($slug));
	}

	function forum($slug = null)
	{
		if ($slug != null)
		{
			$this->ForumCategory->ForumForum->recursive = -1;
			$forum = $this->ForumCategory->ForumForum->findBySlug($slug);
			$this->paginate = array('ForumThread' =>
 							array(
 								'contain' => array('User',
													'ForumPost' => array('User', 'order' => 'ForumPost.created DESC', 'limit' => 1),
 													'ForumUnread' => array('conditions' => array('ForumUnread.user_id' => $this->Auth->user('id'))))
 						));


			$this->set('breadcrumbs', $this->ForumCategory->ForumForum->fetchBreadcrumbs($slug));
			$this->set('threads', $this->paginate('ForumThread', array('ForumThread.forum_forum_id' => $forum['ForumForum']['id'])));
			$this->set('subForums', $this->ForumCategory->ForumForum->fetchSubForums($slug));
			$this->set('forum', $forum);
		}
		else
		{
			$this->redirect(array('controller' => 'forums', 'plugin' => 'forums', 'action' => 'index'));
		}
	}

	function thread($slug = null)
	{
		if ($slug != null)
		{
			$this->ForumThread->recursive = -1;
			$thread = $this->ForumThread->findBySlug($slug);

			$this->ForumThread->id = $thread['ForumThread']['id'];
			$this->ForumThread->saveField('views', $thread['ForumThread']['views'] + 1);
			$this->ForumThread->ForumUnread->deleteAll(array('ForumUnread.forum_thread_id' => $thread['ForumThread']['id'], 'ForumUnread.user_id' => $this->Auth->user('id')));

			$this->set('breadcrumbs', $this->ForumThread->fetchBreadcrumbs($slug));
			$this->set('posts', $this->paginate('ForumPost', array('ForumPost.forum_thread_id' => $thread['ForumThread']['id'])));
			$this->set('thread', $thread);
		}
		else
		{
			$this->redirect(array('controller' => 'forums', 'plugin' => 'forums', 'action' => 'index'));
		}
	}

	function newTopic($forumSlug = null)
	{
		if ($forumSlug != null)
		{
			$forum =  $this->ForumCategory->ForumForum->findBySlug($forumSlug);

			if (isset($this->data))
			{
				$this->data['ForumPost'][0]['title'] = $this->data['ForumThread']['title'];
				$this->data['ForumPost'][0]['text'] = $this->data['ForumThread']['post'];
				$this->data['ForumPost'][0]['tags'] = $this->data['ForumThread']['tags'];
				$this->data['ForumPost'][0]['user_id'] = $this->Auth->user('id');
				$this->data['ForumThread']['user_id'] = $this->Auth->user('id');
				$this->data['ForumThread']['forum_forum_id'] = $forum['ForumForum']['id'];

				if ($this->ForumThread->saveAll($this->data))
				{
					$thread = $this->ForumThread->read();

					$this->redirect(array('action' => 'thread', $thread['ForumThread']['slug']));
				}
			}
			else
			{
				$this->set(compact('forumSlug'));
				$this->set('breadcrumbs', $this->ForumCategory->ForumForum->fetchBreadcrumbs($forumSlug));
				$this->set('forum',$forum);
			}
		}
		else
		{
			$this->redirect(array('controller' => 'forums', 'plugin' => 'forums', 'action' => 'index'));
		}
	}

	function reply($threadSlug = null)
	{
		if ($threadSlug != null && isset($this->data))
		{
			$thread = $this->ForumThread->findBySlug($threadSlug);

			$this->data['ForumPost']['forum_thread_id'] = $thread['ForumThread']['id'];
			$this->data['ForumPost']['user_id'] = $this->Auth->user('id');

			if ($this->ForumPost->save($this->data))
			{
				$this->redirect(array('action' => 'thread', $threadSlug));
			}
		}
		elseif ($threadSlug != null && !isset($this->data))
		{

		}
		else
		{

		}
	}

 	function autoTag()
	{
		App::import('Component', 'Keywords');
		$keywords = new KeywordsComponent();

		echo $keywords->keywordIt($this->params['form']['text']);

		exit;
	}

 }
?>